<?php
namespace app\api\controller;

use app\common\model\User as UserModel;
use app\common\validate\User as UserValidate;

class Account extends Base
{
	public function index()
	{
		$username = request_input('username','');
		$password = request_input('password','');
		$user = UserModel::where(['username'=>$username])->find();
		if($user){
			if($user->password != $password){
				return cdm(500, 0, '登录失败,密码错误!');
			}
			cookie('token', $user->token);
			return cdm(200, 1, '登录成功!');
		}
		return cdm(500, 0, '登录失败,帐号不存在!');
	}

	public function create()
	{
		
	}

	public function save()
	{
		$data['username'] = request_input('username','');
		$data['password'] = request_input('password','');
		$data['password_confirm'] = request_input('password_confirm','');
		$data['name'] = request_input('name','');
		$data['phone'] = request_input('phone',0);
		$data['email'] = request_input('email','');
		$data['qq'] = request_input('qq','');
		$data['wechat'] = request_input('wechat','');
		$data['desc'] = request_input('desc','');

		$validate = new UserValidate;
		if (!$validate->check($data)) {
			return cdm(500, 0, $validate->getError());
		}

		$user = UserModel::where(['username'=>$data['username']])->find();
		if($user){
			return cdm(500, 0, '错误,该帐号已被注册!');
		}
		$user = new UserModel;
		$user->username = $data['username'];
		$user->password = $data['password'];
		$user->name = $data['name'];
		$user->phone = $data['phone'];
		$user->email = $data['email'];
		$user->save();
		return cdm(200, 1, '注册成功!');
	}

	public function read($id)
	{

	}

	public function edit($id)
	{

	}

	public function update($id)
	{
		
	}

	public function delete($id)
	{

	}
}